<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.jobname" placeholder="岗位名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="findList">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="addCompany">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table
      :data="list"
      highlight-current-row
      v-loading="listLoading"
      style="width: 100%;"
      height="70vh"
    >
      <el-table-column prop="jobcode" label="岗位编号" align="center"></el-table-column>
      <el-table-column prop="jobname" label="岗位名称" align="center"></el-table-column>
      <el-table-column prop="jobrank" label="岗位等级" align="center"></el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button type="danger" size="small" @click="handEdit(scope.row)">编辑</el-button>
          <el-button size="small" @click="configure(scope.row)">配置角色</el-button>
          <el-button size="small" @click="handDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        :total="total"
        style="float:right;"
        :current-page="page"
      ></el-pagination>
    </el-col>
    <!--新增 编辑界面-->
    <el-dialog title="岗位" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <stationEdit @hideTotas="hideTotas" v-if="addFormVisible" :sendFormData="sendFormData"></stationEdit>
    </el-dialog>
    <!-- 权限配置 -->
    <el-dialog title="配置角色" :visible.sync="powerVisible" :close-on-click-modal="false">
      <el-form :model="form">
        <el-form-item label="分配角色">
          <el-select v-model="form.roleId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.roleId"
              :label="item.name"
              :value="item.roleId"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="powerVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureRole">确 定</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import stationEdit from "./components/stationEdit";
import {
  omJobList,
  menuList,
  omJobDel,
  roleSetRoleToOmjob,
  getRoleList
} from "../../api/baseSetting";

export default {
  data() {
    return {
      filters: {
        jobname: ""
      },
      //角色配置
      form: {
        pkOmJob: "",
        roleId: ""
      },
      options: [], //角色路由
      addFormVisible: false,
      powerVisible: false, //权限配置弹出
      list: [],
      total: 0,
      page: 1,
      pageSize: 10,
      listLoading: false,
      sendFormData: {
        //点中的公司参数
      }
    };
  },
  components: {
    stationEdit
  },
  methods: {
    //点击删除
    handDelete(e) {
      let pkOmJob = e.pkOmJob;
      this.$confirm("确认删除该角色", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          omJobDel({ pkOmJob }).then(res => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.filters.jobname = "";
            this.page = 1;
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "fail",
            message: "删除失败!"
          });
          this.page = 1;
          this.filters.jobname = "";
          this.getList();
        });
    },
    //点击编辑
    handEdit(detail) {
      this.sendFormData = detail;
      this.addFormVisible = true;
    },
    //点击权限分配
    configure(e) {
      console.log(e);
      this.powerVisible = true;
      this.form.pkOmJob = e.pkOmJob;
    },
    //分页
    handleCurrentChange(val) {
      this.page = val;
      this.getList();
    },
    //隐藏新增表单
    hideTotas() {
      this.addFormVisible = false;
      this.filters.jobname = "";
      this.page = 1;
      this.getList();
    },
    //点击新增
    addCompany() {
      this.sendFormData = {};
      this.addFormVisible = true;
    },
    //点击查询
    findList() {
      this.page = 1;
      this.getList();
    },
    //获取角色列表
    getList() {
      let para = {
        jobname: this.filters.jobname,
        page: {
          page: this.page,
          pageSize: this.pageSize
        }
      };
      this.listLoading = true;
      omJobList(para)
        .then(res => {
          this.listLoading = false;
          this.list = res.records;
          this.total = res.total;
        })
        .catch(err => {
          this.listLoading = false;
        });
    },
    //获取菜单列表  用于分配权限
    getRole() {
      let para = {
        page: {
          page: this.page,
          pageSize: 10000
        }
      };
      getRoleList(para).then(res => {
        this.options = res.records;
      });
    },
    //点击确认 分配角色
    sureRole() {
      roleSetRoleToOmjob(this.form).then(res => {
        this.$message({
          type: "success",
          message: "成功"
        });
        this.powerVisible = false;
        this.page = 1;
        this.form = {
          pkOmJob: "",
          roleId: ""
        };
        this.getList();
      });
    }
  },
  mounted() {
    this.getList();
    this.getRole();
  }
};
</script>
<style lang="less" scoped>
</style>
